var salt = sessionStorage.getItem('usersalt');
var arr = new Array(); //图片id
var arr1 = new Array(); //文件id
layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;

	getnoclear();

	function getnoclear() {

		$.ajax({
			url: url + 'perilInfo/getInfo',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt,
				id: "1027093807343919105"
			},
			success: function(data) {
				if(data.code == 0) {
					var list = data.data.perilInfo;

					$('#finduser').text(list.findUser); //发现人
					$('#findtime').text(list.findTime); //发现时间
					$('#typeId').text(list.typeId); //隐患类型
					$('#describe').text(list.describe); //隐患描述
					for(var i = 0; i < list.attachments.length; i++) {
						//    					$('#despic').append('<img src="'+url+"attachment/seekFile/"+list.attachments[i].id+'" width="150px" height="150px" />');
						$('#despic').append('<div style="display:inline-block;position:relative;margin:10px;" ><img id="pic' + list.attachments[i].id + '" onclick="lookbigpic(\'' + list.attachments[i].id + '\')" src="' + url + 'attachment/seekFile/' + list.attachments[i].id + '" width="100px" height="100px" class="picsty"/></div>')
					}

					$('#auditUser').text(list.auditUser); //核查人
					$('#auditTime').text(list.auditTime); //核查时间
					$('#finishTime').text(list.finishTime); //防控措施完成时间
					$('#auditFruit').text(list.auditFruit); //核查结论
					$('#auditStep').text(list.auditStep); //防控措施
					$('#leaderUser').text(list.leaderUser); //责任人
					$('#leaderUnits').text(list.leaderUnits); //责任单位
					$('#entTime').text(list.entTime); //治理措施落实时间
					$('#program').text(list.program); //治理方案

					var list1 = data.data.perilBills;
					for(var i = 0; i < list1.length; i++) {
						if(list1[i].type == 1) {

							$('#leaderUser1').text(list1[i].leaderUser); //销项核查人
							$('#leaderUnits1').text(list1[i].leaderUnitsName); //核查人单位
							$('#entTime1').text(list1[i].entTime); //核查时间

							$('#typetext').text('未销项');
							/*取出文件 */
							for(var k = 0; k < list1[i].attachmentsFile.length; k++) {
								$('#riskfile').append('<a href="' + url + "attachment/seekFile/" + list1[i].attachmentsFile[k].id + '">' + list1[i].attachmentsFile[k].originaName + '</a>');
							}
							/*取出图片*/
							for(var h = 0; h < list1[i].attachmentsImg.length; h++) {
								$('#riskafterpic').append('<img id="' + list1[i].attachmentsImg[h].id + '" src="' + url + "attachment/seekFile/" + list1[i].attachmentsImg[h].id + '" width="150px" height="150px" />');
							}
						}

					}

				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});

	}

	//*获取所有企业*/
	riskcompay();

	function riskcompay() {
		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt
			},
			success: function(data) {
				$('#dutycompay2').empty();
				if(data.code == 0) {
					var list = data.data;
					var opt = '<option value="">请选择</option>'
					for(var i = 0; i < list.length; i++) {
						opt += '<option value="' + list[i].id + '" >' + list[i].name + '</option>'
					}
					$('#dutycompay2').append(opt);
					form.render();
				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}

	/*增加治理方案*/
	$('#upsubmit').on('click', function() {

		addgovernparti();
	});

	function addgovernparti() {
		var leaderUser2 = $('#leaderUser2').val(); //负责人
		var dutycompay2 = $('#dutycompay2').val(); //责任单位
		var practic2 = $('#practic2').val(); //落实时间
		var treatment2 = $('#treatment2').val() //治理方案

		$.ajax({
			url: url + 'perilInfo/addBill',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt,
				id: "1027092179677143041",
				leaderUser: leaderUser2, //负责任人、核查人
				leaderUnits: dutycompay2, //负责单位、核查单位
				entTime: practic2, //落实时间、核查时间
				program: treatment2, //方案
				type: '1',
				result: '1',
				attachmentsImg: arr.toString(), //图片id
				attachmentsFile: arr1.toString() //附件id
			},
			success: function(data) {

			},
			error: function(error) {
				console.log("获取失败");
			}
		});

	}

	var flg1 = 0;
	/*上传图片*/
	$('#updatafile').change(function() {
		var formData = new FormData();
		formData.append('updatafile', $('#updatafile')[0].files[0]);
		if($('#updatafile')[0].files[0] != null) {
			$.ajax({
				url: url + 'attachment/upload',
				type: 'POST',
				dataType: 'json',
				cache: false,
				data: formData,
				processData: false,
				contentType: false
			}).done(function(res) {

				if(res.code == 0) {
					flg1++;
					arr1.push(res.data.id);

					var img = $('<div style="display:inline-block;position:relative;margin-right:30px;" id="picupfile' + flg1 + '"  ><div onclick="delfile(\'picupfile' + flg1 + '\',\'' + flg1 + '\',\'' + res.data.id + '\')" style="position:absolute;top:-2px;right:-21px;color:red;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><a href="' + url + 'attachment/seekFile/' + res.data.id + '" >' + res.data.originaName + '</a></div>');
					$('#picapp1').append(img);

				}

			}).fail(function(res) {

			});
		}

	});

	var flg = 0;
	$("#selfile").change(function() {

		var formData = new FormData();
		formData.append('selfile', $('#selfile')[0].files[0]);
		if($('#selfile')[0].files[0] != null) {
			$.ajax({
				url: url + 'attachment/upload',
				type: 'POST',
				dataType: 'json',
				cache: false,
				data: formData,
				processData: false,
				contentType: false
			}).done(function(res) {

				if(res.code == 0) {
					flg++;
					arr.push(res.data.id);

					var img = $('<div style="display:inline-block;position:relative;margin:10px;" id="picupdata' + flg + '"  ><div onclick="delpic(\'picupdata' + flg + '\',\'' + flg + '\',\'' + res.data.id + '\')" style="position:absolute;top:-4px;right:-5px;color:white;background:red;border:1px solid black;border-radius:50%;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><img id="pic' + res.data.id + '" onclick="lookbigpic(\'' + res.data.id + '\')" src="' + url + 'attachment/seekFile/' + res.data.id + '" width="100px" height="100px" class="picsty"/></div>');
					$('#picapp').append(img);

				}

			}).fail(function(res) {

			});
		}

	});

});

/*删除事件*/
function delpic(self, num, inde) {
	layer.confirm('你确定要删除吗？', {
		btn: ['确定', '取消'] //按钮
	}, function() {
		$('#' + self).remove();
		for(var i = 0; i < arr.length; i++) {
			if(arr[i] == inde) {
				arr.splice(i, 1);
			}
		}

		layer.msg('删除成功');
	}, function() {

	});
}

/*删除事件*/
function delfile(self, num, inde) {
	layer.confirm('你确定要删除吗？', {
		btn: ['确定', '取消'] //按钮
	}, function() {
		$('#' + self).remove();
		for(var i = 0; i < arr1.length; i++) {
			if(arr1[i] == inde) {
				arr1.splice(i, 1);
			}
		}

		layer.msg('删除成功');
	}, function() {

	});
}

function lookbigpic(self) {

	var json = {
		"status": 1,
		"msg": "",
		"title": "",
		"id": 8,
		"start": 0,
		"data": [{
			"alt": "",
			"pid": 109,
			"src": url + "attachment/seekFile/" + self,
			"thumb": ""
		}]
	}
	layer.photos({
		photos: json,
		anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
	});

}